from services.base_common_service import BaseCommonService


class BaseDevCommonService(BaseCommonService):
    """
    开发环境公共业务
    """

    def db_config(self) -> dict[str, str]:
        from config.config import CONFIG
        if not CONFIG:
            raise Exception("请先加载配置文件,如 @load_global_config('../config/config.json5')")
        c = CONFIG.get('dev')['mysql']
        return {
            'host': c['host'],
            'port': c['port'],
            'username': c['username'],
            'password': c['password'],
            'database': c['database'],
            'mysql_log': c['log_sql']
        }

    def __init__(self, model_class, time_field=None):
        super().__init__(model_class, time_field)
        self.model_class = model_class
        self.time_field = time_field

    def delete(self, start_time, end_time):
        session = self.get_session()
        time_condition = getattr(self.model_class, self.time_field).between(start_time, end_time)
        session.query(self.model_class).filter(time_condition).delete()
        session.commit()
